package com.sg.zhui.visitor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sg.zhui.visitor.R;
import com.sg.zhui.visitor.http.AsyncRequest;
import com.sg.zhui.visitor.http.BaseStringCallback_Host;
import com.sg.zhui.visitor.http.Setting;
import com.sg.zhui.visitor.http.httpContxt;
import com.sg.zhui.visitor.utils.PreferencesHelper;
import com.sg.zhui.visitor.utils.SmsButtonUtil;
import com.sg.zhui.visitor.utils.StringUtil;
import com.sg.zhui.visitor.utils.TelNumMatch;
import com.sg.zhui.visitor.widget.EditTextClearAble;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by chenlipeng on 2018/7/19 0019
 * describe:  验证码
 */
public class LoginActivity extends AppCompatActivity implements AsyncRequest {

//    @BindView(R.id.iv_titlebar_left_visitor)
//    ImageView ivTitlebarLeft;
//    @BindView(R.id.tv_titlebar_title_visitor)
//    TextView tvTitlebarTitle;
//    @BindView(R.id.iv_pic)
//    ImageView ivPic;
//    @BindView(R.id.rl_title_bg)
//    RelativeLayout rlTitleBg;
//    @BindView(R.id.et_phone)
//    EditTextClearAble etPhone;
//    @BindView(R.id.tv_phone)
//    TextView tvPhone;
//    @BindView(R.id.et_code)
//    EditTextClearAble etCode;
//    @BindView(R.id.tv_verification_code)
//    TextView tvVerificationCode;

    private SmsButtonUtil mSmsButtonUtil;
    private PreferencesHelper preferencesHelper;
    private String verification = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
//        ButterKnife.bind(this);
//        tvTitlebarTitle.setText("登录");
//        ivTitlebarLeft.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                finish();
//            }
//        });

        // 验证码倒计时工具
//        mSmsButtonUtil = new SmsButtonUtil(tvVerificationCode);
//        mSmsButtonUtil.setCountDownText("重新获取（%ds）");
//
//        preferencesHelper = new PreferencesHelper(LoginActivity.this);

    }

//    @OnClick({R.id.tv_verification_code, R.id.tv_login})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.tv_login:
//                String code = etCode.getText().toString().trim();
//                if (!StringUtil.isEmpty(code)) {
//                    if (code.equals(verification)) {
//                        Toast.makeText(this, "验证成功", Toast.LENGTH_SHORT).show();
//
//                        Intent intent1 = new Intent(LoginActivity.this, VisitorMainActivity.class);
//                        startActivity(intent1);
//                    } else {
//                        Toast.makeText(this, "验证码错误", Toast.LENGTH_SHORT).show();
//                    }
//                }
//                break;
//            case R.id.tv_verification_code:
//                String phoneNum = etPhone.getText().toString().trim();
//
//                if (TextUtils.isEmpty(phoneNum)) {
//                    Toast.makeText(LoginActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                boolean mobileNO = TelNumMatch.isMobileNO(phoneNum);
//                if (!mobileNO) {
//                    Toast.makeText(LoginActivity.this, "请输入有效的手机号", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                if (TextUtils.isEmpty(phoneNum)) {
//                    Toast.makeText(LoginActivity.this, "名字不能为空", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                verificationCode(phoneNum, "");
//                break;
//
//        }
//    }

    /**
     * 验证码
     */
    public void verificationCode(String userTel, String userName) {
        String url = Setting.sendVerificationUrl();

        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("userTel", userTel);
        stringStringHashMap.put("userName", userName);

        OkHttpUtils
                .post().params(stringStringHashMap)
                .url(url)
                .id(httpContxt.SEND_VERIFICATION_URL)
                .build()
                .execute(new BaseStringCallback_Host(LoginActivity.this, this));
    }


    @Override
    public void RequestComplete(Object var1, Object data) {
        if (var1.equals(httpContxt.SEND_VERIFICATION_URL)) {//验证码
//        {
//            "userId" : "135964958e9e4f9b8b92ededa16e4dbe",
//                "type" : "0",
//                "state" : true,
//                "msg" : "验证码已发送，请及时查收.",
//                "verification" : "1423"
//        }
            try {
                JSONObject jsonObject = new JSONObject((String) data);
//                JSONObject data1 = jsonObject.getJSONObject("data");
                String userId = jsonObject.getString("userId");
                verification = jsonObject.getString("verification");

                if (!StringUtil.isEmpty(userId)) {
                    preferencesHelper.saveUserId(userId);

//                    tvVerificationCode.setEnabled(false);
                    mSmsButtonUtil.startCountDown();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }


    }

    @Override
    public void RequestError(Object var1, int var2, String var3) {

    }
}
